//
//  HSDSpiderman.m
//  GoldenCard
//
//  Created by 正合适 on 2018/3/21.
//  Copyright © 2018年 合时代. All rights reserved.
//

#import "HSDSpidermanZ.h"
#import "HSDSpiderMacro.h"
#import "HSDSpidermanMonitor.h"
#import "HSDSpidermanBaseInfoHelper.h"
#import "HSDSpidermanNetworkInfoHelper.h"
#import "HSDSpiderDataUploader.h"
#import "HSDSpidermanLocationHelper.h"
#import "HSDSpidermanCustomHelper.h"
#import "HSDSpidermanTools.h"

@interface HSDSpidermanZ ()

/**spiderman的运行线程*/
@property (nonatomic, strong) NSThread *mainThread;
/**监听器*/
@property (nonatomic, strong) HSDSpidermanMonitor *monitor;
/**基础信息*/
@property (nonatomic, strong) HSDSpidermanBaseInfoHelper *baseInfoHelper;
/**网络*/
@property (nonatomic, strong) HSDSpidermanNetworkInfoHelper *netHelper;
/**<#desc#>*/
@property (nonatomic, strong) HSDSpiderDataUploader *uploader;
/**定位*/
@property (nonatomic, strong) HSDSpidermanLocationHelper *locationHelper;
/**自定义数据*/
@property (nonatomic, strong) HSDSpidermanCustomHelper *customHelper;

@end

@implementation HSDSpidermanZ
#pragma mark - ---------- API ----------
#pragma mark - 开启服务
+ (void)run {
    [[HSDSpidermanZ shareInstance] runThread];
}

+ (void)customDataWithKey:(NSString *)key val:(NSString *)jsonVal {
    [[HSDSpidermanZ shareInstance] customKey:key jsonVal:jsonVal];
}

+ (void)savePhone:(NSString *)phone {
    [[HSDSpidermanZ shareInstance] customKey:kUserPhone jsonVal:phone];
}

#pragma mark - ---------- 生命周期 ----------
#pragma mark - 单例
+ (instancetype)shareInstance {
    static HSDSpidermanZ *manager = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        manager = [[HSDSpidermanZ alloc] init];
    });
    return manager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _uploader = [[HSDSpiderDataUploader alloc] init];
        
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)addObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeAction) name:UIApplicationDidBecomeActiveNotification object:nil];
}

#pragma mark - 开启一个线程
- (void)runThread {
    if (!_mainThread) {
        _mainThread = [[NSThread alloc] initWithTarget:self selector:@selector(mainThreadAction) object:nil];
        [_mainThread setName:kSpidermanThreadName];
        [_mainThread start];
    }
}

#pragma mark - 当前线程Action
- (void)mainThreadAction {
    [self addObservers];
    [self initialize];
    @autoreleasepool {
        // 线程保活
        NSRunLoop *mainRunloop = [NSRunLoop currentRunLoop];
        [mainRunloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [mainRunloop run];
    }
}

#pragma mark - 初始化数据
- (void)initialize {
    _monitor = [[HSDSpidermanMonitor alloc] initWithThread:_mainThread];
    _baseInfoHelper = [[HSDSpidermanBaseInfoHelper alloc] init];
    _netHelper = [[HSDSpidermanNetworkInfoHelper alloc] init];
    _locationHelper = [[HSDSpidermanLocationHelper alloc] init];
    
    _uploader.baseInfo = _baseInfoHelper.baseInfoData;
    _uploader.networkInfoModel = _netHelper.netModel;
    _uploader.locationModel = _locationHelper.locationModel;
    
    // 读取基础数据
    [self readBaseInfo];
    // 开启监听器
    [self startMonitor];
    // 监听网络信息
    [_netHelper startRead];
        
    [_locationHelper startLocation];
    
}

#pragma mark - 开启监听
- (void)startMonitor {
    [_monitor startMonitor];
}

#pragma mark - 开启网络信息监听
- (void)startNetMonitor {
    [_netHelper startRead];
}

#pragma mark - 读取基础数据
- (void)readBaseInfo {
    [_baseInfoHelper startRead];
}

- (void)applicationDidBecomeAction {
    [_netHelper startRead];
    [_locationHelper startLocation];
}

- (void)customKey:(NSString *)key jsonVal:(NSString *)jsonVal {
    if (!_customHelper) {
        _uploader.customModel = self.customHelper.customModel;;
    }
    [self.customHelper customInfoWithKey:key val:jsonVal];
}

- (HSDSpidermanCustomHelper *)customHelper {
    if (!_customHelper) {
        _customHelper = [[HSDSpidermanCustomHelper alloc] init];
    }
    return _customHelper;
}

@end
